#define MAIN

#include <grub.h>
#include <printThings.h>
#include <gdt.h>
#include <interrupt.h>
#include <keyboard.h>
#include <floppy.h>
#include <memory.h>
#include <filesystem.h>
#include <string.h>
#include <multitasking.h>
#include <elf.h>
#include <mytime.h>
#include <sharedMem.h>
#include <kernelWork.h>


void kernel_init(struct multibootInfo *mbInfo) {
    cls();
    printf("OS1    ");
    printf("PM->OK\n");

    printf("MultibootInfoFlags: %x\n", mbInfo->flags);

    printf("Initialisiere physisches Memory Management...");
    initPhysMemManagement(mbInfo);
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("Initialisiere Globale Deskriptor Tabelle...");
    initGdt();
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("Lade Globale Deskriptor Tabelle...");
    loadGdt();
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("Lade Interrupts...");
    initInt();
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("Initialisiere keyboard...");
    initKeyboard();
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("Initialisiere Paging!\n");
    initPaging();
    setColor(GREEN);
    printf("[Paging initialisiert]\n");
    setStandard();

    printf("teste printf %d %c %x %s\n", 12346789, 'c', 123, "test");

    printf("init heap!");
    initHeap();
    setColor(GREEN);
    printf("[Heap initialisiert]\n");
    setStandard();

    printf("starte interrupts...");
    asm volatile ("sti");
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("initTimer...");
    setTimerFrequenz(100);
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("initialisiere Floppy!\n");
    initFloppy();
    printf("init Filesystem...");
    initFilesystem();
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("init Multitasking (Start kernelProzess)...");
    initMultitasking();
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    printf("init Shared Memory...");
    initSharedMemory();
    setColor(GREEN);
    printf("[OK]\n");
    setStandard();

    /*
    printf("%x\n", mbInfo->vbe_control_info);
    printf("%x\n", mbInfo->vbe_mode_info);
    printf("%x\n", mbInfo->vbe_mode);
    printf("%x\n", mbInfo->vbe_interface_seg);
    printf("%x\n", mbInfo->vbe_interface_off);
    printf("%x\n", mbInfo->vbe_interface_len);
    */

    //printf("read: #|TEXTE|TEXT (this is the os1 path)\n\n");
    //MyFile f = decodePathToFile("#|TEXTE|TEXT");
    //readFile(f.sektor, f.size);

    //printf("lade Programm: #|PROGS|PROG2\n");
    //loadElfBinary("#|PROGS|PROG2\0");
    printf("Lade Konsole...\n");
    loadElfBinary("#|PROGS|PROG1\0");
    printf("unlock SharedMem\ns");
    unlockSharedMem();
    //loadElfBinary("#|PROGS|PROG3\0");


    //diskInfo();
    //readRootDir();
    //testBitOps();
    /*
    unsigned int * pointer = 0x100000;
    unsigned int make_a_page_fault = *pointer;
    */
    while (1) {
        /*der Kernel macht seine Arbeit*/
        //processStdout();
        //exitProgramm();
    }
}
